﻿// Accord Audio Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

namespace Accord.Audition
{
    using Accord.MachineLearning;
    using Accord.Math;
    using System;
    using System.Collections.Generic;
    using Accord.Compat;
    using Accord.Audio;

    /// <summary>
    ///   Base class for <see cref="BagOfAudioWords">Bag of Visual Words</see> implementations.
    /// </summary>
    /// 
    /// <seealso cref="BagOfAudioWords"/>
    /// <seealso cref="BagOfAudioWords{TPoint, TFeature, TClustering, TExtractor}"/>
    /// <seealso cref="BagOfAudioWords{TPoint, TFeature}"/>
    /// <seealso cref="BagOfAudioWords{TPoint}"/>
    /// 
    [Serializable]
    public class BaseBagOfAudioWords<TModel, TFeature, TPoint, TClustering, TExtractor> :
        // TODO: Unify with Accord.MachineLearning.BaseBagOfWords
        BaseBagOfWords<TModel, TFeature, TPoint, TClustering, TExtractor, Signal>,
        IBagOfWords<string>,
        IBagOfWords<Signal>,
        IUnsupervisedLearning<TModel, string, int[]>,
        IUnsupervisedLearning<TModel, string, double[]>,
        IUnsupervisedLearning<TModel, Signal, int[]>,
        IUnsupervisedLearning<TModel, Signal, double[]>
        where TFeature : IFeatureDescriptor<TPoint>
        where TModel : BaseBagOfAudioWords<TModel, TFeature, TPoint, TClustering, TExtractor>
        where TClustering : IUnsupervisedLearning<IClassifier<TPoint, int>, TPoint, int>
        where TExtractor : IAudioFeatureExtractor<TFeature>
    {
        /// <summary>
        ///   Constructs a new <see cref="BagOfAudioWords"/>.
        /// </summary>
        /// 
        protected BaseBagOfAudioWords()
        {
        }


        /// <summary>
        /// Applies the transformation to a set of input vectors,
        /// producing an associated set of output vectors.
        /// </summary>
        /// <param name="input">The input data to which
        /// the transformation should be applied.</param>
        /// <param name="result">The location to where to store the
        /// result of this transformation.</param>
        /// <returns>The output generated by applying this
        /// transformation to the given input.</returns>
        public double[] Transform(string input, double[] result)
        {
            using (Signal signal = Signal.FromFile(input))
                return Transform(signal, result);
        }

        /// <summary>
        /// Applies the transformation to a set of input vectors,
        /// producing an associated set of output vectors.
        /// </summary>
        /// <param name="input">The input data to which
        /// the transformation should be applied.</param>
        /// <param name="result">The location to where to store the
        /// result of this transformation.</param>
        /// <returns>The output generated by applying this
        /// transformation to the given input.</returns>
        public int[] Transform(string input, int[] result)
        {
            using (Signal signal = Signal.FromFile(input))
                return Transform(signal, result);
        }

        /// <summary>
        /// Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// <returns>The output generated by applying this transformation to the given input.</returns>
        public double[] Transform(string input)
        {
            return Transform(input, new double[NumberOfWords]);
        }

        int[] ICovariantTransform<string, int[]>.Transform(string input)
        {
            return Transform(input, new int[NumberOfWords]);
        }

        /// <summary>
        /// Applies the transformation to a set of input vectors,
        /// producing an associated set of output vectors.
        /// </summary>
        /// <param name="input">The input data to which
        /// the transformation should be applied.</param>
        /// <returns>The output generated by applying this
        /// transformation to the given input.</returns>
        public double[][] Transform(string[] input)
        {
            return Transform(input, Jagged.Zeros(input.Length, NumberOfWords));
        }

        int[][] ICovariantTransform<string, int[]>.Transform(string[] input)
        {
            return Transform(input, Jagged.Zeros<int>(input.Length, NumberOfWords));
        }

        /// <summary>
        /// Applies the transformation to a set of input vectors,
        /// producing an associated set of output vectors.
        /// </summary>
        /// <param name="input">The input data to which
        /// the transformation should be applied.</param>
        /// <param name="result">The location to where to store the
        /// result of this transformation.</param>
        /// <returns>The output generated by applying this
        /// transformation to the given input.</returns>
        public double[][] Transform(string[] input, double[][] result)
        {
            For(0, input.Length, (i, detector) =>
            {
                using (Signal signal = Signal.FromFile(input[i]))
                    Transform(detector.Transform(signal), result[i]);
            });

            return result;
        }

        /// <summary>
        /// Applies the transformation to a set of input vectors,
        /// producing an associated set of output vectors.
        /// </summary>
        /// <param name="input">The input data to which
        /// the transformation should be applied.</param>
        /// <param name="result">The location to where to store the
        /// result of this transformation.</param>
        /// <returns>The output generated by applying this
        /// transformation to the given input.</returns>
        public int[][] Transform(string[] input, int[][] result)
        {
            For(0, input.Length, (i, detector) =>
            {
                using (Signal signal = Signal.FromFile(input[i]))
                    Transform(detector.Transform(signal), result[i]);
            });

            return result;
        }




        /// <summary>
        /// Learns a model that can map the given inputs to the desired outputs.
        /// </summary>
        /// <param name="x">The model inputs.</param>
        /// <param name="weights">The weight of importance for each input sample.</param>
        /// <returns>A model that has learned how to produce suitable outputs
        /// given the input data <paramref name="x" />.</returns>
        public TModel Learn(string[] x, double[] weights = null)
        {
            return InnerLearn(x, weights, (xi, detector) =>
            {
                using (Signal signal = Signal.FromFile(xi))
                    return detector.Transform(signal);
            });
        }

        double[] IBagOfWords<string>.GetFeatureVector(string value)
        {
            throw new NotImplementedException();
        }

        double[] IBagOfWords<Signal>.GetFeatureVector(Signal value)
        {
            throw new NotImplementedException();
        }
    }
}
